#include <QCoreApplication>
#include <event2/event.h>
#include <event2/listener.h>
#include <iostream>
#include <signal.h>
#include <QDebug>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <thread>

void read_file(evutil_socket_t fd,short event,void* arg){
    char buf[1024] = {0};//初始化，不用在结尾处添加 '\0'
    int len = read(fd,buf,sizeof(buf) - 1);
    if(len > 0){
        qDebug() << buf;
    }else{
        qDebug() << ".";
        usleep(500000);
    }

}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    if(signal(SIGPIPE,SIG_IGN) == SIG_ERR){
        return -1;
    }
    event_config* conf = event_config_new();
    event_config_require_features(conf,EV_FEATURE_FDS);
    event_base* base = event_base_new_with_config(conf);
    event_config_free(conf);
    if(base){
        //只读非阻塞方式打开文件
        int sock = open("/var/log/auth.log",O_RDONLY | O_NONBLOCK,0);
        if(sock <= 0){
            qDebug() << "open fail";
            return 0;
        }
        //文件指针移到结尾
        lseek(sock,0,SEEK_END);
        event* fev = event_new(base,sock,EV_READ | EV_PERSIST,read_file,event_self_cbarg());
        event_add(fev,0);

        event_base_dispatch(base);
    }

    return 0;
}
